stream-cancel
This crate provides multiple mechanisms for interrupting a Stream
.
Stream combinator
The extension trait [StreamExt
] provides a single new Stream
combinator: take_until
.
[StreamExt::take_until
] continues yielding elements from the underlying Stream
until a
Future
resolves, and at that moment immediately yields None
and stops producing further
elements.
For convenience, the crate also includes the [Tripwire
] type, which produces a cloneable
Future
that can then be passed to take_until
. When a new Tripwire
is created, an
associated [Trigger
] is also returned, which interrupts the Stream
when it is dropped.
extern crate tokio;
use ;
use *;
let listener = bind.unwrap;
let = new;
let mut rt = new.unwrap;
rt.spawn;
// tell the listener to stop accepting new connections
drop;
rt.shutdown_on_idle.wait.unwrap;
Stream wrapper
Any stream can be wrapped in a [Valved
], which enables it to be remotely terminated through
an associated [Trigger
]. This can be useful to implement graceful shutdown on "infinite"
streams like a TcpListener
. Once [Trigger::close
] is called on the handle for a given
stream's [Valved
], the stream will yield None
to indicate that it has terminated.
extern crate tokio;
use Valved;
use *;
use thread;
let listener = bind.unwrap;
let = new;
let server = spawn;
// the server thread will normally never exit, since more connections
// can always arrive. however, with a Valved, we can turn off the
// stream of incoming connections to initiate a graceful shutdown
drop;
server.join.unwrap;
You can share the same [Trigger
] between multiple streams by first creating a [Valve
],
and then wrapping multiple streams using [Valve::Wrap
]:
extern crate tokio;
use Valve;
use *;
let = new;
let listener1 = bind.unwrap;
let listener2 = bind.unwrap;
let incoming1 = valve.wrap;
let incoming2 = valve.wrap;
let mut rt = new.unwrap;
rt.spawn;
// the runtime will not become idle until both incoming1 and incoming2 have stopped
// (due to the select). this checks that they are indeed both interrupted when the
// valve is closed.
drop;
rt.shutdown_on_idle.wait.unwrap;